package backend.project.gym_management.base.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Slf4j
public class RoleInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");
        String requestURI = request.getRequestURI();
        log.debug(role);
        // 放行公共资源和 Knife4j 相关路径
        if (isPublicOrKnife4jResource(requestURI)) {
            return true;
        }

        // 未登录用户只能访问登录页面
        if (role == null) {
            response.sendRedirect("http://localhost:9090/login");
            return false;
        }

        // 检查角色是否有权限访问当前路径
        if (!hasPermission(role, requestURI)) {
            // 无权限访问，重定向到对应角色的首页或登录页
            redirectToRoleHome(response, role);
            return false;
        }

        return true;
    }

    private boolean isPublicOrKnife4jResource(String uri) {
        return uri.startsWith("/login.html") ||
                uri.startsWith("/auth/") ||
                uri.startsWith("/css/") ||
                uri.startsWith("/js/") ||
                uri.startsWith("/static/") ||
                uri.startsWith("/error") ||
                // Knife4j 相关路径
                uri.startsWith("/doc.html") ||
                uri.startsWith("/webjars/") ||
                uri.startsWith("/v2/api-docs") ||
                uri.startsWith("/swagger-resources") ||
                uri.startsWith("/swagger-ui.html") ||
                uri.equals("/favicon.ico");
    }

    private boolean hasPermission(String role, String uri) {
        switch (role) {
            case "管理员":
                return uri.startsWith("/admin");
            case "教练":
                return uri.startsWith("/coach");
            case "普通员工":
                return uri.startsWith("/staff");
            case "用户":
                return uri.startsWith("/user");
            default:
                return false;
        }
    }

    private void redirectToRoleHome(HttpServletResponse response, String role) throws Exception {
        log.debug(role);
        switch (role) {
            case "管理员":
                response.sendRedirect("http://localhost:9090/admin");
                break;
            case "教练":
                response.sendRedirect("http://localhost:9090/coach");
                break;
            case "普通员工":
                response.sendRedirect("http://localhost:9090/staff");
                break;
            case "用户":
                response.sendRedirect("http://localhost:9090/user");
                break;
            default:
                response.sendRedirect("http://localhost:9090/admin");
        }
    }
}